package controller

import (
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"

	"soma-server/pkg/e"
	r "soma-server/response"
	"soma-server/service/k8s"
	"soma-server/vo"
	"soma-server/common"
)

type IK8sPodController interface {
	Pods(c *gin.Context)
}

type K8sController struct {
	PodService k8s.IPodService
}

func NewK8sController() IK8sPodController {
	podService := k8s.NewPodService()
	return &K8sController{
		PodService: podService,
	}
}

func (k *K8sController) Pods(c *gin.Context) {
	var podReq vo.PodListRequest
	// 参数绑定
	if err := c.ShouldBind(&podReq); err != nil {
		common.Log.Errorln(err.Error())
		r.Response(c, http.StatusBadRequest, e.BINDING_PARAMS, nil, false)
		return
	}
	// 参数校验
	if err := common.Validate.Struct(&podReq); err != nil {
		errStr := err.(validator.ValidationErrors)[0].Translate(common.Trans)
		common.Log.Errorln(errStr)
		r.Response(c, http.StatusBadRequest, e.INVALID_PARAMS, nil, false)
		return
	}
	
	common.Log.Infoln(podReq)
	pods, err := k.PodService.GetPods(&podReq)
	if err != nil {
		common.Log.Errorf("List Pods Err:", err)
		r.Response(c, http.StatusBadRequest, e.ERROR_GET_POD_LIST, nil, false)
		return
	}

	r.Response(c, http.StatusOK, e.SUCCESS_GET_POD_LIST, gin.H{
		"pods": pods,
	}, true)
}
